package workexp.alg.question;

import org.junit.Test;

import java.util.Arrays;

public class ReverseWords {
    public String reverseWords(String s) {
        char[] workload = s.toCharArray();
        int j=0;

        for (int i = 0; i <= workload.length; i++) {
            if(i==workload.length||workload[i]==" ".toCharArray()[0]){
                int end=i-1;
                reverseString(workload,j,end);
                j=i+1;
            }
        }
        return String.valueOf(workload);

    }

    public void reverseString(char[] s,int start,int end) {
        int i =start;
        int j = end;

        while (i<j){
            char tmp;
            tmp = s[i];
            s[i]=s[j];
            s[j]=tmp;
            i++;
            j--;
        }
    }

    @Test
    public void test(){
        String s= "Let's take LeetCode contest";
        String res = reverseWords(s);
        System.out.println(res);
    }
}
